package editer;

import data.AppData;
import sys.io.File;
import js.html.IFrameElement;
import haxe.io.Path;
import haxe.ui.containers.Box;

/**
 * 单个编辑界面
 */
class EditFileView extends Box {
	/**
	 * 是否处于预览状态
	 */
	public var preview:Bool = true;

	public var frame:IFrameView;

	public var url:String;

	public var isLoaded = false;

	public var callback:Void->Void;

	public function onLoad(cb:Void->Void):Void {
		this.callback = cb;
		if (isLoaded) {
			if (callback != null)
				callback();
			callback = null;
		}
	}

	public function new(path:String):Void {
		super();
		this.url = path;
		this.text = path == null ? "未命名" : Path.withoutDirectory(path);
		frame = new IFrameView();
		this.addComponent(frame);
		frame.url = "./monaco/editor.html";
		frame.percentWidth = 100;
		frame.percentHeight = 100;
		this.percentWidth = this.percentHeight = 100;

		frame.getIFrame().onload = () -> {
			isLoaded = true;
			trace("路径", AppData.currentProject.rootPath);
			untyped frame.getIFrame().contentWindow.window.setProjectPath(AppData.currentProject.rootPath, new FileSystem());
			untyped frame.getIFrame().contentWindow.window.registerZProjectData(AppData.currentProject);
			untyped frame.getIFrame().contentWindow.window.setCodeValue(path, File.getContent(path));
			if (callback != null)
				callback();
			callback = null;
		}
	}
}
